/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: QueueListener
 * Author:   pc
 * Date:     2019/9/5 14:37
 * Description: Queue监听器
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.example.rabbitmqdemo.testthread;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * 〈功能：〉<br> 
 * 〈Queue监听器〉
 * @author liuhaoran
 * @create 2019/9/5 14:37
 * @since 1.0.0
 */
@Component
@Slf4j
public class QueueListener implements ApplicationListener<ContextRefreshedEvent> {

	@Autowired
	private MockQueue mockQueue;

	@Autowired
	private DeferredResultHolder deferredResultHolder;


	@Override
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

		new Thread(()->{
			while(true){
				//判断CompleteOrder字段是否是空
				if (StringUtils.isNotBlank(mockQueue.getCompleteOrder())){
					String orderNumber = mockQueue.getCompleteOrder();
					deferredResultHolder.getMap().get(orderNumber).setResult("success");
					log.info("返回订单处理结果");
					//将CompleteOrder设为空，表示处理成功
					mockQueue.setCompleteOrder(null);
				}else {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
}